<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <view class="name titleBold"> 您的血压情况 </view>
      <view class="inputBox">
        <span class="name lineHeight86">高压</span>
        <input
          id="SBP"
          v-model="mainData.SBP"
          type="digit"
          class="uni-input lineHeight86 marginR120"
          name="SBP"
          placeholder-class="phcolor"
        />
        <view class="unit">mmHg</view>
        <view class="clear"></view>
      </view>
      <view class="inputBox">
        <span class="name lineHeight86">低压</span>
        <input
          v-model="mainData.DBP"
          type="digit"
          class="uni-input lineHeight86 marginR120"
          name="DBP"
          placeholder-class="phcolor"
        />
        <view class="unit">mmHg</view>
        <view class="clear"></view>
      </view>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import { checkInt, checkBetween } from '@/common/assessKeyCheck';
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import { mapGetters } from 'vuex';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    sickTitleCom
  },
  data() {
    return {
      mainData: {
        SBP: '',
        DBP: ''
      }
    };
  },
  computed: {
    ...mapGetters(['active', 'assessForm', 'isHaveDiabetes'])
  },
  mounted() {},
  methods: {
    async nextBtn() {
      // 先校验数据
      let isPassAuth = true;
      // ① 必填校验
      Object.keys(this.mainData).forEach((key) => {
        if (!this.mainData[key]) {
          uni.showToast({ title: `请输入所有题目`, icon: 'none' });
          isPassAuth = false;
          return;
        }
      });
      if (!isPassAuth) {
        return false;
      }
      // ②录入值格式校验
      if (checkInt(this.mainData.SBP)) {
        uni.showToast({ title: `高压数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.SBP, '高压', 50, 250)) {
        uni.showToast({
          title: checkBetween(this.mainData.SBP, '高压', 50, 250),
          icon: 'none'
        });
        return false;
      }
      if (checkInt(this.mainData.DBP)) {
        uni.showToast({ title: `低压数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.DBP, '低压', 30, 250)) {
        uni.showToast({
          title: checkBetween(this.mainData.DBP, '低压', 50, 250),
          icon: 'none'
        });
        return false;
      }
      // 提交vuex assessForm值
      let submitData = this.assessForm;
      Object.keys(this.mainData).forEach((key) => {
        submitData[key] = this.mainData[key];
      });
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      const healthData = await ajaxSend(this.mainData);
      let curActive = this.active + 1;
      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      } else {
        this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    }
  }
};
</script>
